<?php

namespace App\Http\Controllers;

use App\Models\feedModel;
use App\service\Audit;
use App\service\JwtService;
use App\service\QiniuUploade;
use App\service\ReturnService;
use App\service\ReviewImages;
use Illuminate\Http\Request;

class feedController extends Controller
{
    /*
     * 反馈图片上传
     * */
    public function imgUploade(Request $request)
    {

        //获取图片名称和临时路径
        $name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        //判断图片是否合规
        $res = new ReviewImages();
        $state = $res->verify($tmp_name);
        if ($state != '不合规') {
            $res = QiniuUploade::uploade($name, $tmp_name);
            return ReturnService::successReturn(200, '图片上传成功', $res);
        }
        return ReturnService::failedReturn(400, 'The picture is not compliant. ');
    }

    /*
     * 反馈信息提交
     * */
    public function feedTarget(Request $request)
    {
        $data = $request->post();
        $audit = Audit::audit($data['content']);
        htmlspecialchars($data['content']);
        //判断内容是否合规
        if ($audit['conclusion'] == '不合规') {
            return ReturnService::failedReturn(400, $audit['conclusion']);
        }
        //解析token
        $user = JwtService::decodeToken($data['token']);
        $user_id = $user->data;
        $data['user_id'] = $user_id;
        $data['picture'] = json_encode($data['picture']);
        unset($data['token']);
        //数据入库
        $res = feedModel::create($data);
        if ($res) {
            return ReturnService::successReturn(200, '提交成功', []);
        }
        return ReturnService::failedReturn(400, '提交失败');
    }
}
